package com.softeng.network.chess;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Random;

public class Client implements Runnable
{
	private static final int PORT = 4444;
	protected Socket conn;
	protected boolean connected;
	private String ID = null;
	
	public Client()
	{
		ID = String.valueOf( new Random().nextInt(100000) );
		
		System.out.println( "[CLIENT] - Client Generated with ID: " + ID );
		try
		{
			conn = new Socket( "127.0.0.1", PORT );
			connected = true;
			Thread t = new Thread( this );
			t.start();
		}
		catch( IOException e)
		{
			e.printStackTrace();
		}
	}
	
	@Override
	public void run()
	{

	}
	
	public void sendMessage( NetworkCommand cmd )
	{
		System.out.println( "[CLIENT] - Sending a message...");
		
		try 
		{
			ObjectOutputStream oos = new ObjectOutputStream(conn.getOutputStream());
			oos.writeObject( cmd.generateCommand() );
			oos.flush();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public String getClientID()
	{
		return ID;
	}
}
